/**
 * 用户信息修改页面JS
 * User: fengchuang
 * Date: 15-8-17
 * Time: 下午5:58
 * To change this template use File | Settings | File Templates.
 */
$(function(){
    //激活左侧导航菜单
    activeNavBar("MANAGE_USER_INFO");
    initSelect2();

    $('#file').ace_file_input({
        style:'well',
        btn_choose:'点击选择图片',
        btn_change:null,
        no_icon:'ace-icon fa fa-picture-o',
        thumbnail:'large',
        droppable:true,
        allowExt: ['jpg', 'jpeg', 'png', 'gif'],
        allowMime: ['image/jpg', 'image/jpeg', 'image/png', 'image/gif']
        // before_remove:function () {
        //     $("#userAvatarFlag").val("1");
        //     return true;
        // },
        // before_change:function(files, dropped){
        //     if($("#userAvatar").val() != ""){
        //         $("#userAvatarFlag").val("1");
        //     }
        //     if(!isImageFile(files)) {
        //         return false;
        //     }
        //     return true;
        // }
    });
    $("#saveBtn").click(function(){
        save();
    });
    $("#goBackBtn, #headGoBackBtn").click(function(){
        window.forward(springUrl+ "/web/userInfo/index");
    });
    initData();
});

function initData(){
    jQuery.ajax({
        type: "POST",
        dataType: "json",
        url: springUrl+"/web/userInfo/detail",
        data: {
            uniqueNo: $('#uniqueNo').val()
        },
        success: function (data) {
            if(StringUtils.isNotBlank(data.userAvatar)){
                var imgPath = springUrl+"/web/fileStorageInfo/download?uniqueNo="+data.userAvatar+"&"+new Date().getTime();;
                $('#file').ace_file_input('show_file_list', [{type: 'image', name: data.userAvatar, path: imgPath}]);
            }
            $("#userAccount").val(data.userAccount);
            $("#userAvatar").val(data.userAvatar);
            $("#userName").val(data.userName);
            $("#userPhone").val(data.userPhone);
        },
        error: function(data){
            Alert.error("初始化数据失败!");
        }
    });
}

/**
 * 保存
 */
function save(){
    if($('#editForm').trigger("validate").isValid()){
        var index =  layer.msg('正在处理中...', {icon: 16, shade: 0.3, time: 0});
        var formData = new FormData();
        formData.append("file", $("#file")[0].files[0]);
        formData.append("uniqueNo", $("#uniqueNo").val());
        formData.append("userAvatar", $("#userAvatar").val());
        formData.append("userName", $("#userName").val());
        formData.append("userPhone", $("#userPhone").val());
        jQuery.ajax({
            type: "POST",
            url: springUrl+"/web/userInfo/update",
            data: formData,
            async: false,
            processData : false,
            contentType : false,
            success:function (data) {
                layer.close(index);
                Alert.success("数据保存成功!", function(){
                    window.forward(springUrl+ "/web/userInfo/index");
                });
            },
            error : function(data){
                layer.close(index);
                Alert.error(data.responseText);
            }
        });
    }
}